package com.bwie.user.service.impl;

import com.bwie.common.utils.R;
import com.bwie.user.conf.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.UserDao;
import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private JwtUtil jwtUtil;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * 1.3登录（10分）
     * 使用JWT完成登录（5分）
     * 在Gateway网关进行JWT校验拦截，解析HTTP请求头当中携带的JWT信息，验证用户是否登录（5分）
     * @param user
     * @return
     */
    @Override
    public R login(UserEntity user) {
        UserEntity userEntity = baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("uname", user.getUname()));
        if (userEntity!=null&&userEntity.getPwd().equals(user.getPwd())){
            String token = jwtUtil.createJWT(userEntity.getUid() + "", userEntity.getUname(), "user");
            return R.ok().setData(token).put("user",userEntity);
        }
        return R.error();
    }

}